<template>
  <div class="bgc-default pad-20">
    <div class="bg-color-white pad-20">
      <div>
        <el-button type="primary" @click="saveMemberBrade()">新增</el-button>
      </div>
      <div class="top-operate-default mar-t-20 mar-b-20">
        <div class="search-item">
          <el-input @keyup.enter.native="handleSearch" v-model="keyworkedName" placeholder="请输入等级名称" style="width: 300px; margin-right: 10px"></el-input>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </div>
      </div>
      <!-- 内容列表 -->
      <div class="table-container">
        <el-table ref="multipleTable" :data="entityList" v-loading="listLoading" element-loading-text="加载中" style="width: 100%">
          <el-table-column label="等级名称">
            <template slot-scope="scope">
              <span @click="saveMemberBrade(scope.row)" class="cursor-p">{{ scope.row.gradeName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="店主数" prop="memberNum"></el-table-column>
          <el-table-column label="佣金比例" prop="commissionRate">
            <template slot-scope="scope">
              {{ scope.row.commissionRate }}%
            </template>
          </el-table-column>
          <el-table-column width="140px" label="操作" align="right" fixed="right">
            <template slot-scope="scope">
              <el-button type="text" @click="saveMemberBrade(scope.row)">编辑</el-button>
              <el-button type="text" @click="delEntity(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 列表分页 -->
      <div class="pagination">
        <el-pagination background @size-change="handleSizeChange"
                       @current-change="handleCurrentChange" :current-page="entityListQuery.currentPage"
                       :page-sizes="[10, 20, 50, 100]" :page-size="entityListQuery.size" layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
      </div>
    </div>
    <!--新增-->
    <el-dialog title="" :visible.sync="entityVisible" width="650px">
      <el-form ref="form" :model="entity" :rules="rules" label-position="right" label-width="100px" style='width: 500px; margin-left:50px;'>
        <el-form-item label="等级名称" prop="gradeName">
          <el-input v-model="entity.gradeName" style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item label="佣金比例" prop="commissionRate">
          <el-input placeholder="请输入" v-model="entity.commissionRate" style="width: 200px">
            <template slot="append">%</template>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveEntity">确认</el-button>
        <el-button @click="entityVisible = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { pageMemberGrade, saveMemberGrade, delMemberGrade } from '@/api/user/memberGrade'
  export default {
    data() {
      return {
        brandType: '0',
        entityList: [],
        total: null,
        keyworkedName: '',
        entityListQuery: {
          currentPage: 1,
          size: 10,
          enterpriseId: this.getEnterpriseId()
        },
        listCategory: [],
        listLoading: false,
        entity: {
          memberGradeId: '',
          enterpriseId: this.getEnterpriseId(),
          gradeName: ''
        },
        entityVisible: false,
        rules: {
          gradeName: [
            { required: true, message: '请填写等级名称', trigger: 'blur' },
            { max: 32, message: '等级名称最多输入32个字符' }
          ],
          commissionRate: [
            { required: false, message: '请填默认佣金比例', trigger: 'blur' },
            { pattern: /(^(\d|[1-9]\d|100)(\.\d{1,2})?$)/, message: '请输入正确佣金比例' }
          ]
        }
      }
    },
    created() {
      this.initPage()
    },
    methods: {
      handleClick() {
        this.entityListQuery = {
          currentPage: 1,
          size: 10,
          enterpriseId: this.getEnterpriseId()
        }
        if (this.brandType === '1') {
          this.listBrandCategory()
        }
        this.initPage()
      },
      handleSearch() {
        this.initPage()
      },
      handleReset() {
        this.entityListQuery = {
          currentPage: 1,
          size: 10,
          enterpriseId: this.getEnterpriseId()
        }
        this.keyworkedName = ''
        this.initPage()
      },
      delEntity(row) {
        delMemberGrade(row.memberGradeId).then(response => {
          if (response.isError) return
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.initPage()
        })
      },
      saveMemberBrade(row) {
        this.entity = {}
        if (row) {
          this.entity = JSON.parse(JSON.stringify(row))
        }
        this.entityVisible = true
      },
      saveEntity() {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.entity.enterpriseId = this.getEnterpriseId()
            saveMemberGrade(this.entity).then(response => {
              if (response.isError) return
              this.$message({
                message: '保存成功',
                type: 'success'
              })
              this.initPage()
              this.entityVisible = false
            })
          }
        })
      },
      initPage() {
        this.listLoading = true
        this.entityListQuery['gradeName'] = this.keyworkedName
        pageMemberGrade(this.entityListQuery).then(response => {
          if (response.isError) return
          this.entityList = response.data.records
          this.total = response.data.total
          this.listLoading = false
        })
      },
      // 更改当前页数
      handleCurrentChange(val) {
        this.entityListQuery.currentPage = val
        this.initPage()
      },
      // 更改当前页面大小
      handleSizeChange(val) {
        this.entityListQuery.size = val
        this.initPage()
      },
      // 勾选
      handleSelectionChange(val) {
        this.multipleSelection = val
      }
    }
  }
</script>

<style>
  .user-avatar {
    width: 68px;
    height: 68px;
    display: block;
    padding: 1px;
    border: #efefef 1px solid;
  }
</style>